package littlewhisker.utils {
	import littlewhisker.globals.constants.eventtypes.LWEventTypes;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 * @author Philip
	 */
	public class Looper extends EventDispatcher {
		
		private var _useFrames:Boolean 	= false;
		private var _interval:Number	= 20;
		private var _status:Boolean		= false;
		
		private var timer:Timer;
		
		public function get useFrames() : Boolean {
			return _useFrames;
		}
		
		public function set useFrames(useFrames : Boolean) : void {
			_useFrames = useFrames;
		}
		
		public function get interval() : Number {
			return _interval;
		}
		
		public function set interval(interval : Number) : void {
			_interval = interval;
		}
		
		public function get status() : Boolean {
			return _status;
		}
		
		public function set status(status : Boolean) : void {
			_status = status;
		}
		
		public function Looper():void{
			
		}
		
		public function start() : void {
			if(useFrames) addEventListener(Event.ENTER_FRAME, onLoop);	

			if(useFrames){
				timer = new Timer(interval);
				timer.start();
				timer.addEventListener(TimerEvent.TIMER, onLoop);
			}
			status = true;
		}

		public function stop() : void {
			if(useFrames) removeEventListener(Event.ENTER_FRAME, onLoop);	
			
			if(!useFrames){
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, onLoop);
			}
		}
		
		private function onLoop( event:Event ) : void {
			dispatchEvent(new Event(LWEventTypes.ON_LOOP));
		}
	}
}
